import request from '@/utils/request'
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min'

const publicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhe78TDXpWf0dEzYdMXjWe3sWw8GPBEHzU56Zq3KLTNRuGSTMB2woj85NkNrfIpF/FN6COS11ZjSeI71aEILsrfs3G4yg842tPVJWIg8AyPZuAFgOpqyZoMKZLHEldMnhyZDPDBB+AK6m2XeCkd6tbs1eiGUA+zo1fsUGJSMfT0tJbD+fl6DfmOZDTG3kJ2IJD/FR+Wsf0NUI6g5jXnC4Q1HO5CuZZaO6Bw04K893Z3uH8aEU4wBN9YxO5waB3oklsY39qIDy65pEwKoUO+Nnq/t+oGZ20OArrQboxrbiMrtJuSRjd/5e+JWTfXKFto2NMpOHPJltj3HHsfWZ2gWXlwIDAQAB';

// jsencrypt 加密
export function encrypt1(txt) {
	const encryptor = new JSEncrypt();
	encryptor.setPublicKey(publicKey); // 设置公钥
	return encryptor.encrypt(txt); // 对数据进行加密
}

//获取随机uuid
export function getUuid () {
  if (typeof crypto === 'object') {
    if (typeof crypto.randomUUID === 'function') {
      return crypto.randomUUID();
    }
    if (typeof crypto.getRandomValues === 'function' && typeof Uint8Array === 'function') {
      const callback = (c) => {
        const num = Number(c);
        return (num ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))).toString(16);
      };
      return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, callback);
    }
  }
  let timestamp = new Date().getTime();
  let perforNow = (typeof performance !== 'undefined' && performance.now && performance.now() * 1000) || 0;
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    let random = Math.random() * 16;
    if (timestamp > 0) {
      random = (timestamp + random) % 16 | 0;
      timestamp = Math.floor(timestamp / 16);
    } else {
      random = (perforNow + random) % 16 | 0;
      perforNow = Math.floor(perforNow / 16);
    }
    return (c === 'x' ? random : (random & 0x3) | 0x8).toString(16);
  });
};